<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Borrow extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		
	}
	
	/**
	 * 标列表
	 * @param $ftype 标类型
	 */
	function index(){
		//获取标列表
		$this->load->model('Borrow_model');
		//资金用途
		$vdata['borrow_fuse'] 				= $this->Borrow_model->arrays('borrow_fuse');
		//标的安全等级
		$this->load->model('Conset_model');
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();

		// 正在借款中的标的
		$mobile_bor_passing = 'mobile_bor_passing';
		//$mobile_bor_passing_list = $this->mycache->get_array($mobile_bor_passing);//缓存
		if (!$mobile_bor_passing_list){
			// 正在借款中的标
			$adata['page_size']  = 15;
			$adata['page_start'] = 0;
			$adata['where']['is_check'] = 1;
			$adata['where']['is_verify'] = 1;
			$adata['where']['flag'] = "passing";
			$mobile_bor_passing_list= $this->Borrow_model->get_zhs_index_borrow($adata);
			$this->mycache->set_array($mobile_bor_passing,$mobile_bor_passing_list,60);
		}
		$vdata['borrow_list'] = $mobile_bor_passing_list['borrow_list'];
		
		// 待发布上线中的标的
		$mobile_bor_going = 'mobile_bor_going';
		$mobile_bor_going_list = $this->mycache->get_array($mobile_bor_going);//缓存
		if (!$mobile_bor_going_list){
			$adata['page_size']  = 10;
			$adata['page_start'] = 0;
			$adata['where']['is_check'] = 1 ;
			$adata['where']['is_verify'] = 1;
			$adata['where']['flag'] = "going";
			$mobile_bor_going_list= $this->Borrow_model->get_zhs_index_borrow($adata);
			$this->mycache->set_array($mobile_bor_going,$mobile_bor_going_list,60);
		}
		$vdata['wait_borrow_list'] = $mobile_bor_going_list['borrow_list'];
		//推广人员
		$invite = $this->input->get('invite');
		if ($invite){
			setcookie("inviteRequest",$invite, time()+3600);
		}
		
		$vdata['tab'] = 1;
		$this->load->view('zhs_mobile/index', $vdata);
	}
	
	// 标的详情
	function obj($id){
		$user = $this->session->userdata('rsh_user');//获取用户session信息
		if($user == null){//如果未登录就跳到登录页面
			header('Location: /zhs_mobile/login');
			exit;
		}
		$this->load->model('User_model');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
		$this->load->model('Conset_model');
		$vdata['borrow_bill_bank'] 				= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$vdata['borrow_fsafe'] 					= $this->Conset_model->get_credit_title_list();
		$vdata['borrow_style'] 					= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_work_time'] 				= $this->Borrow_model->arrays('borrow_work_time');//起息时间
		
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		if(!$borrow_obj){
			$ret['msg'] =  "理财标不存在";
			$ret['url_next'] = "/zhs_mobile/borrow/index.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		$vdata['borrow'] = $borrow_obj;
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		
		// 增加点击次数
		//$this->Borrow_model->update_hits($id);
		
		//当前用户信息
		$user_id 							= $user['user_id'];
		$user_obj 							= $this->User_model->get_by_uid($user_id);
		$vdata['user_obj'] 					= $user_obj;
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type1');//标种
		$vdata['user_acnt'] = $this->Account_model->get_account($user_id);
		if(!$vdata['user_acnt']){// 账户无信息
			$initAccount = array(
				'user_id'      => $user_id,
				'total'        => '0',// 账户总额
				'use_money'    => '0',// 可用
				'no_use_money' => '0',// 冻结
				'collection'   => '0',// 回款
				'cps_money'   => '0',// 推广
				'red_money'   => '0',// 代金券，代金卷
			);
			$account_id = $this->account->init_account($initAccount);
			if($account_id) {
				$initAccount['id'] = $account_id;
			}
			$vdata['user_acnt'] = $initAccount;
		}
		// 已投资用户信息
		$borrow_tender_list = $this->Borrow_model->get_borrow_tender_2(array('select'=>2,
			'where'=>array('borrow_id'=>$id),page_start=>'0',page_size=>'10'));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = $user_obj['username'];
			$borrow_tender_list1[] 		= $tender;
		}
		$vdata['borrow_tender_list'] = $borrow_tender_list1;
		$vdata['page'] = count($borrow_tender_list1);
		
		$vdata['tab'] = 1;
		$this->load->view('zhs_mobile/xq', $vdata);
	}
	
	/**
	 * 更多投资记录
	 * @param int $page
	 */
	function tz_log_more($page = 0){
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$id = $this->input->get("id");
		
		// 已投资用户信息
		$borrow_tender_list = $this->Borrow_model->get_borrow_tender_2(array('select'=>2,
			'where'=>array('borrow_id'=>$id),page_start=>intval($page),page_size=>'10'));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = strdisplay_start($user_obj['username'],2);
			$tender['add_time'] = date("Y-m-d",$tender['add_time']);
			if(($tender['money'] == $tender['account']) && $tender['fstatus'] != 3){
				$tender['reslt'] = "成 功";
			}elseif($tender['fstatus'] == 3){
				$tender['reslt'] = "已撤回";
			}else{
				$tender['reslt'] = "成 功";
			}
			$borrow_tender_list1[] 		= $tender;
		}
		echo json_encode($borrow_tender_list1);
	}
	
	/**
	 * 投标
	 */
	function tender(){
		$id 				= $this->input->post("id");
		$account 			= $this->input->post("account");
		$paypassword 		= $this->input->post("paypassword");
		$red_money 			= $this->input->post("red_money");
		//$dxb_pwd 			= $this->input->post("dxb_pwd")?$this->input->post("dxb_pwd"):"";
		
		//TODO ****** by liub 20141212 代金券
		$red_money 		= $this->input->post("red_money");
		//$dxb_pwd 			= $this->input->post("dxb_pwd")?$this->input->post("dxb_pwd"):"";

		$user = $this->session->userdata('rsh_user');
		if($user == null){//如果未登录就跳到登录页面
			header('Location: /zhs_mobile/login');
			exit;
		}
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');
		$user = $this->User_model->get_username_by_id($user['user_id']);
		if($user['paypassword']  == ""){
			$ret['msg']= "请先设置支付密码";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if($user['real_status']  != "1"){
			$ret['msg']=  "请先通过实名认证";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		//获取标信息
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		if(!$borrow_obj){
			$ret['msg'] =  "理财标不存在";
			$ret['url_next'] = "/zhs_mobile/borrow/index.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		//验证支付密码
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		//验证提交参数
		$ret['msg'] = '';
		if(!$this->myformcheck->isNumber($account)){
			$ret['msg'] =  "投标金额必须为整数，且为标的最小投资金额的整数倍";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if($pay_result == '' || $pay_result == 0){
			$ret['msg'] = "支付密码不正确";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		// ****** by liub 
		if( $account % $borrow_obj['lowest_account'] !=0 ){
			$ret['msg'] = "投标金额必须为标的最小投资金额的整数倍";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if(!$this->myformcheck->IsDouble($account)){
			$ret['msg'] = "投标金额不正确";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
	
		if($borrow_obj['fstatus'] != 1){
			$ret['msg'] = "此标不能投";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}	
		
		/*** TODO 20140109 by liub 注释 启标时间*/
		/*if((time()-$borrow_obj['bill_starttime']) > 0){
			echo "此标已过启标时间";
			return;
		}*/
		
		if($borrow_obj['account'] == $borrow_obj['account_yes']){
			$ret['msg'] = "此标已满";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if($borrow_obj['lowest_account'] > $account){
			$ret['msg'] = "请输入正确的金额";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if(($borrow_obj['most_account'] > 0) && ($account -$borrow_obj['most_account'])>0){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		/*if($borrow_obj['is_dxb'] == 1 && $dxb_pwd != $borrow_obj['dxb_pwd']){
			$ret['msg'] = "定向密码不正确";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}*/
		if($borrow_obj['user_id'] == $user['user_id']){
			$ret['msg'] = "不能投自己的标";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		$user_tender_all = $this->Borrow_model->get_borrow_tender(array('where'=>array('user_id'=>$user['user_id'],'borrow_id'=>$id)));
		
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		
		/** 删除用户单笔限制条件
		$user_tender_account = 0;
		foreach($user_tender_all as $tender_obj){
			$user_tender_account += $tender_obj['account'];	
		}
		if(($borrow_obj['most_account'] > 0) && (($account+$user_tender_account) > $borrow_obj['most_account'])){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		*/
		// 封装参数
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];
		$data['borrow_user_id'] = $borrow_obj['user_id'];
		$data['borrow_id'] 	= $id;
		
		$user_account = $this->Account_model->get_account($user['user_id']);
		if($account > $user_account['use_money']){
			$ret['msg'] = "您的余额不足,请先充值";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
				
		// *** begin 20150204 by liub 新增：投标时确认用户宝付账户中余额
		/* $balance_arr = self::queryBalance($user['user_id']);
		$balance = 0;
		if($balance_arr['code'] == 'CSD000'){
			$balance  = $balance_arr['balance'];
		}else{
			$ret['msg'] = "投标失败,".$balance_arr['msg'];
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if($balance == -1){
			$ret['msg'] = "投标失败，请稍候再试";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if($balance - $user_account['use_money'] < 0){
			$ret['msg'] = "您的账户异常，请联系客服";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if($balance - $account < 0 ){
			$ret['msg'] = "投标失败，请联系客服";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		} */
		//**** end  资金验证完毕
		
		
		
		
		$account = round($account, 2);
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		
		//使用代金卷
		if(isset($red_money) && $red_money == "1"){
			$red = floor($account / 200);
			$red = max(0, $red);
			$red = min($red, $user_account['red_money']);
			/*begin tangc 20150104 限制代金卷的激活*/
			//获取激活代金卷总额
			$this->load->model('Accountlog_model', 'accountlog');
			$red_sum = $this->accountlog->get_sum_type($user['user_id'],REDPACKAGE_DEDUCT);
			$red_sum = 0;	//屏蔽代金卷激活次数
			$money_sum  = $red_sum*200;
			$key_money = $user_account['total'] - $money_sum;
			if($key_money > 0){
				if($key_money > $user_account['use_money']) $key_money = $user_account['use_money'];
			}
			else $key_money = 0;
			$key_red = floor($key_money/200);
			$red = min($key_red,$red);
			/*end */
			$data['red_money'] 	= $red;
		}
		
		$data['account'] 	= $account;
		
		//投标
		$flag = $this->Borrow_model->borrow_tender($data);
		if($flag == 1){
			//发放代金券
			//$this->load->model('Redmanagement_model', 'redmanagement');
			//$this->redmanagement->send_red($user['user_id'],6,"投标".$id."期获得奖励");
			//$title = "<a  href='/borrow/zhs_obj/".$data['borrow_id']."'>第".$data['borrow_id']."期</a>";
			//$this->send_msg(array('user_id'=>$user['user_id'],'sys_id'=>25,'money'=>$account,'title'=>$title));//购买成功站内信
			
			$ret['msg'] = "投标成功";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}else{
			$ret['msg'] = "投标失败";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
	}
	
		/**
	 * 查询余额(服务端接口)
	 */
	private function queryBalance($user_id){
		$config['user_id'] 		= null;
		$this->load->library('baofooSdk/dto/request/BalanceDto',$config,'balanceDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br_recharge');
		// 赋值
		$this->balanceDto->__set("user_id",$user_id);
		return $this->br_recharge->api_AccountBalance($this->balanceDto);
	
	}
	
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Borrow extends CI_Controller {
    
	function __construct(){
		parent::__construct();
		
	}
	
	/**
	 * 标列表
	 * @param $ftype 标类型
	 */
	function index(){
		//获取标列表
		$this->load->model('Borrow_model');
		//资金用途
		$vdata['borrow_fuse'] 				= $this->Borrow_model->arrays('borrow_fuse');
		//标的安全等级
		$this->load->model('Conset_model');
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();

		// 正在借款中的标的
		$mobile_bor_passing = 'mobile_bor_passing';
		//$mobile_bor_passing_list = $this->mycache->get_array($mobile_bor_passing);//缓存
		if (!$mobile_bor_passing_list){
			// 正在借款中的标
			$adata['page_size']  = 15;
			$adata['page_start'] = 0;
			$adata['where']['is_check'] = 1;
			$adata['where']['is_verify'] = 1;
			$adata['where']['flag'] = "passing";
			$mobile_bor_passing_list= $this->Borrow_model->get_zhs_index_borrow($adata);
			$this->mycache->set_array($mobile_bor_passing,$mobile_bor_passing_list,60);
		}
		$vdata['borrow_list'] = $mobile_bor_passing_list['borrow_list'];
		
		// 待发布上线中的标的
		$mobile_bor_going = 'mobile_bor_going';
		$mobile_bor_going_list = $this->mycache->get_array($mobile_bor_going);//缓存
		if (!$mobile_bor_going_list){
			$adata['page_size']  = 10;
			$adata['page_start'] = 0;
			$adata['where']['is_check'] = 1 ;
			$adata['where']['is_verify'] = 1;
			$adata['where']['flag'] = "going";
			$mobile_bor_going_list= $this->Borrow_model->get_zhs_index_borrow($adata);
			$this->mycache->set_array($mobile_bor_going,$mobile_bor_going_list,60);
		}
		$vdata['wait_borrow_list'] = $mobile_bor_going_list['borrow_list'];
		//推广人员
		$invite = $this->input->get('invite');
		if ($invite){
			setcookie("inviteRequest",$invite, time()+3600);
		}
		
		$vdata['tab'] = 1;
		$this->load->view('zhs_mobile/index', $vdata);
	}
	
	// 标的详情
	function obj($id){
		$user = $this->session->userdata('rsh_user');//获取用户session信息
		if($user == null){//如果未登录就跳到登录页面
			header('Location: /zhs_mobile/login');
			exit;
		}
		$this->load->model('User_model');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
		$this->load->model('Conset_model');
		$vdata['borrow_bill_bank'] 				= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$vdata['borrow_fsafe'] 					= $this->Conset_model->get_credit_title_list();
		$vdata['borrow_style'] 					= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_work_time'] 				= $this->Borrow_model->arrays('borrow_work_time');//起息时间
		
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		if(!$borrow_obj){
			$ret['msg'] =  "理财标不存在";
			$ret['url_next'] = "/zhs_mobile/borrow/index.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		$vdata['borrow'] = $borrow_obj;
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		
		// 增加点击次数
		//$this->Borrow_model->update_hits($id);
		
		//当前用户信息
		$user_id 							= $user['user_id'];
		$user_obj 							= $this->User_model->get_by_uid($user_id);
		$vdata['user_obj'] 					= $user_obj;
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type1');//标种
		$vdata['user_acnt'] = $this->Account_model->get_account($user_id);
		if(!$vdata['user_acnt']){// 账户无信息
			$initAccount = array(
				'user_id'      => $user_id,
				'total'        => '0',// 账户总额
				'use_money'    => '0',// 可用
				'no_use_money' => '0',// 冻结
				'collection'   => '0',// 回款
				'cps_money'   => '0',// 推广
				'red_money'   => '0',// 代金券，代金卷
			);
			$account_id = $this->account->init_account($initAccount);
			if($account_id) {
				$initAccount['id'] = $account_id;
			}
			$vdata['user_acnt'] = $initAccount;
		}
		// 已投资用户信息
		$borrow_tender_list = $this->Borrow_model->get_borrow_tender_2(array('select'=>2,
			'where'=>array('borrow_id'=>$id),page_start=>'0',page_size=>'10'));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = $user_obj['username'];
			$borrow_tender_list1[] 		= $tender;
		}
		$vdata['borrow_tender_list'] = $borrow_tender_list1;
		$vdata['page'] = count($borrow_tender_list1);
		
		$vdata['tab'] = 1;
		$this->load->view('zhs_mobile/xq', $vdata);
	}
	
	/**
	 * 更多投资记录
	 * @param int $page
	 */
	function tz_log_more($page = 0){
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$id = $this->input->get("id");
		
		// 已投资用户信息
		$borrow_tender_list = $this->Borrow_model->get_borrow_tender_2(array('select'=>2,
			'where'=>array('borrow_id'=>$id),page_start=>intval($page),page_size=>'10'));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = strdisplay_start($user_obj['username'],2);
			$tender['add_time'] = date("Y-m-d",$tender['add_time']);
			if(($tender['money'] == $tender['account']) && $tender['fstatus'] != 3){
				$tender['reslt'] = "成 功";
			}elseif($tender['fstatus'] == 3){
				$tender['reslt'] = "已撤回";
			}else{
				$tender['reslt'] = "成 功";
			}
			$borrow_tender_list1[] 		= $tender;
		}
		echo json_encode($borrow_tender_list1);
	}
	
	/**
	 * 投标
	 */
	function tender(){
		$id 				= $this->input->post("id");
		$account 			= $this->input->post("account");
		$paypassword 		= $this->input->post("paypassword");
		$red_money 			= $this->input->post("red_money");
		//$dxb_pwd 			= $this->input->post("dxb_pwd")?$this->input->post("dxb_pwd"):"";
		
		//TODO ****** by liub 20141212 代金券
		$red_money 		= $this->input->post("red_money");
		//$dxb_pwd 			= $this->input->post("dxb_pwd")?$this->input->post("dxb_pwd"):"";

		$user = $this->session->userdata('rsh_user');
		if($user == null){//如果未登录就跳到登录页面
			header('Location: /zhs_mobile/login');
			exit;
		}
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');
		$user = $this->User_model->get_username_by_id($user['user_id']);
		if($user['paypassword']  == ""){
			$ret['msg']= "请先设置支付密码";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if($user['real_status']  != "1"){
			$ret['msg']=  "请先通过实名认证";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		//获取标信息
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		if(!$borrow_obj){
			$ret['msg'] =  "理财标不存在";
			$ret['url_next'] = "/zhs_mobile/borrow/index.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		//验证支付密码
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		//验证提交参数
		$ret['msg'] = '';
		if(!$this->myformcheck->isNumber($account)){
			$ret['msg'] =  "投标金额必须为整数，且为标的最小投资金额的整数倍";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if($pay_result == '' || $pay_result == 0){
			$ret['msg'] = "支付密码不正确";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		// ****** by liub 
		if( $account % $borrow_obj['lowest_account'] !=0 ){
			$ret['msg'] = "投标金额必须为标的最小投资金额的整数倍";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if(!$this->myformcheck->IsDouble($account)){
			$ret['msg'] = "投标金额不正确";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
	
		if($borrow_obj['fstatus'] != 1){
			$ret['msg'] = "此标不能投";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}	
		
		/*** TODO 20140109 by liub 注释 启标时间*/
		/*if((time()-$borrow_obj['bill_starttime']) > 0){
			echo "此标已过启标时间";
			return;
		}*/
		
		if($borrow_obj['account'] == $borrow_obj['account_yes']){
			$ret['msg'] = "此标已满";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if($borrow_obj['lowest_account'] > $account){
			$ret['msg'] = "请输入正确的金额";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		if(($borrow_obj['most_account'] > 0) && ($account -$borrow_obj['most_account'])>0){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		/*if($borrow_obj['is_dxb'] == 1 && $dxb_pwd != $borrow_obj['dxb_pwd']){
			$ret['msg'] = "定向密码不正确";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}*/
		if($borrow_obj['user_id'] == $user['user_id']){
			$ret['msg'] = "不能投自己的标";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		$user_tender_all = $this->Borrow_model->get_borrow_tender(array('where'=>array('user_id'=>$user['user_id'],'borrow_id'=>$id)));
		
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		
		/** 删除用户单笔限制条件
		$user_tender_account = 0;
		foreach($user_tender_all as $tender_obj){
			$user_tender_account += $tender_obj['account'];	
		}
		if(($borrow_obj['most_account'] > 0) && (($account+$user_tender_account) > $borrow_obj['most_account'])){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		*/
		// 封装参数
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];
		$data['borrow_user_id'] = $borrow_obj['user_id'];
		$data['borrow_id'] 	= $id;
		
		$user_account = $this->Account_model->get_account($user['user_id']);
		if($account > $user_account['use_money']){
			$ret['msg'] = "您的余额不足,请先充值";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
				
		// *** begin 20150204 by liub 新增：投标时确认用户宝付账户中余额
		/* $balance_arr = self::queryBalance($user['user_id']);
		$balance = 0;
		if($balance_arr['code'] == 'CSD000'){
			$balance  = $balance_arr['balance'];
		}else{
			$ret['msg'] = "投标失败,".$balance_arr['msg'];
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if($balance == -1){
			$ret['msg'] = "投标失败，请稍候再试";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if($balance - $user_account['use_money'] < 0){
			$ret['msg'] = "您的账户异常，请联系客服";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
		
		if($balance - $account < 0 ){
			$ret['msg'] = "投标失败，请联系客服";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		} */
		//**** end  资金验证完毕
		
		
		
		
		$account = round($account, 2);
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		
		//使用代金卷
		if(isset($red_money) && $red_money == "1"){
			$red = floor($account / 200);
			$red = max(0, $red);
			$red = min($red, $user_account['red_money']);
			/*begin tangc 20150104 限制代金卷的激活*/
			//获取激活代金卷总额
			$this->load->model('Accountlog_model', 'accountlog');
			$red_sum = $this->accountlog->get_sum_type($user['user_id'],REDPACKAGE_DEDUCT);
			$red_sum = 0;	//屏蔽代金卷激活次数
			$money_sum  = $red_sum*200;
			$key_money = $user_account['total'] - $money_sum;
			if($key_money > 0){
				if($key_money > $user_account['use_money']) $key_money = $user_account['use_money'];
			}
			else $key_money = 0;
			$key_red = floor($key_money/200);
			$red = min($key_red,$red);
			/*end */
			$data['red_money'] 	= $red;
		}
		
		$data['account'] 	= $account;
		
		//投标
		$flag = $this->Borrow_model->borrow_tender($data);
		if($flag == 1){
			//发放代金券
			//$this->load->model('Redmanagement_model', 'redmanagement');
			//$this->redmanagement->send_red($user['user_id'],6,"投标".$id."期获得奖励");
			//$title = "<a  href='/borrow/zhs_obj/".$data['borrow_id']."'>第".$data['borrow_id']."期</a>";
			//$this->send_msg(array('user_id'=>$user['user_id'],'sys_id'=>25,'money'=>$account,'title'=>$title));//购买成功站内信
			
			$ret['msg'] = "投标成功";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}else{
			$ret['msg'] = "投标失败";
			$ret['url_next'] = "/zhs_mobile/borrow/obj/$id.html";
			$this->load->view('zhs_mobile/msg',$ret);
			return;
		}
	}
	
		/**
	 * 查询余额(服务端接口)
	 */
	private function queryBalance($user_id){
		$config['user_id'] 		= null;
		$this->load->library('baofooSdk/dto/request/BalanceDto',$config,'balanceDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br_recharge');
		// 赋值
		$this->balanceDto->__set("user_id",$user_id);
		return $this->br_recharge->api_AccountBalance($this->balanceDto);
	
	}
	
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}